const { createApp } = Vue;
window.VUE = createApp({
  data() {
    return {
      leftData: {},
      acIndex: "",
      oldIndex: "",
      isSave: true,
      centerObj: {
        projectName: {
          label: "项目名称",
          type: "text",
        },
        projectPath: {
          label: "项目本地地址",
          type: "text",
        },
        gitAddress: {
          label: "git地址",
          type: "text",
        },
        gitBranch: {
          label: "分支",
          type: "text",
        },
        dcloudAppkey: {
          label: "打包appkey",
          type: "text",
        },
        packageName: {
          label: "应用包名",
          type: "text",
        },
        alias: {
          label: "证书别名",
          type: "text",
        },
        storepass: {
          label: "证书密码",
          type: "text",
        },
        keystore: {
          label: "线上证书文件名称",
          type: "text",
        },
        build: {
          label: "打包状态",
          type: "select",
        },
        outApk: {
          label: "apk下载地址",
          type: "link",
        },
        outWgt: {
          label: "wgt下载地址",
          type: "link",
        },
      },

      // log
      showLog: false,
      messageObj: {},
      WS: null,
      // sdk
      sdkData: SDK.data,
      // upload
      uploadData: UPLOAD.data,
      // build
      buildData: BUILD.data,
    };
  },
  watch: {
    ...UPLOAD.watch,
    messageObj: {
      handler() {
        this.$nextTick(() => {
          this.$refs[this.acIndex + "box"].scrollTop =
            this.$refs[this.acIndex + "box"].scrollHeight;
        });
      },
      deep: true,
    },
  },
  created() {
    // 初始化ws
    this.WS = new WebSocketProxy(`ws:${location.host}/socket`, this);
    this.WS.create();
    this.init();
    this.initWebworker();
  },

  methods: {
    // 切换项目
    selectItem(item) {
      if (this.acIndex == item.projectName) return;
      this.acIndex = item.projectName;
      fetchGet("/endLog", item).then((res) => {
        if (this.messageObj[this.acIndex]) {
          if (
            dateFormat(new Date(), "yyyy-MM-dd hh:mm") + " : " + res !=
            this.messageObj[this.acIndex][
              this.messageObj[this.acIndex].length - 1
            ]
          )
            this.messageObj[this.acIndex].push(
              dateFormat(new Date(), "yyyy-MM-dd hh:mm") + " : " + res
            );
        } else {
          if (res)
            this.messageObj[this.acIndex] = [
              dateFormat(new Date(), "yyyy-MM-dd hh:mm") + " : " + res,
            ];
        }
      });
    },
    initWebworker(){
      function work(){
        onmessage = ({data:{jobId,message}})=>{ 
          function fetchUpload(url, data) {
            return fetch(url, {
              method: "POST",
              body: data,
            }).then(responseInterceptor);
          }
          // 响应拦截器
          function responseInterceptor(res) {
            return new Promise((resolve, reject) => {
              if (res.status === 200) {
                res.json().then((data) => {
                  if (data.code == 200) {
                    data.data;
                    resolve(data.data);
                  }else if(data.code == 666){
                    message({ type: "info", message: data.msg });
                    resolve(data.data);
                  }else {
                    reject(res);
                    message({ type: "error", message: data.msg });
                  }
                });
              } else {
                message({ type: "error", message: "请求失败" });
                reject(res);
              }
            });
          }
          const formData = new FormData();
          for (const [key, value] of Object.entries(message)) {
            formData.append(key, value);
          }
          fetchUpload(location.origin+"/sdk/uploadchunk", formData)
          .then((res) => {
            postMessage ({jobId, result: 'add'}); 
  
          })
          .catch((err) => {
            postMessage ({jobId, result: 'del'}); 
          });
        }
      }
      this.uploadData.webworker = makeWorker(work);
    },
    ...CRUD,
    ...SDK.methods,
    ...UPLOAD.methods,
    ...BUILD.methods,
  },
}).mount("#app");
